<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html dir="ltr"><head><title>CS-Script Runtime Environment</title>

<meta http-equiv="Content-Type" content="text/html; charset=Windows-1252">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<xml></xml>
<link rel="stylesheet" type="text/css" href="MSDN.css"></head>
<body id="divID" class="dtBODY">
<div id="nsbanner">
<div id="bannerrow1">
<table class="bannerparthead" cellspacing="0">
<tbody>
<tr id="hdr">
<td class="runninghead">
<p align="left">Added asdCS-Script 2.4.1</p>
</td>
<td class="product"> <br>
</td>
</tr>
</tbody>
</table>
</div>
<div id="TitleRow">
<h1 class="dtH1" align="left">CS-Script Runtume
Environment</h1>
</div>
</div>
<div id="nstext">
<p>The script engine is a small runtime environment and as such
it has
some characteristics, which may be accessed/analysed at runtime. The
CSScriptLibrary contains class <span style="color: rgb(51, 51, 153);">CSSEnvironment</span>,
which provides information about the CS-Script runtime environment. Use
the <span style="font-style: italic; color: rgb(51, 51, 153);">CSSEnvironment</span>
class to retrieve information such as:</p>
<p style="margin-left: 40px;"><span style="font-weight: bold;">Properties</span></p>
<ul>
<li><span style="font-style: italic;">Settings</span><br>
<span style="color: rgb(51, 51, 153);">csscript.Settings</span>
object containing the&nbsp;data from the&nbsp;script engine
custom configuration file (eg. <span style="font-style: italic;">css_config.xml</span>).</li>
<li><span style="font-style: italic;">CacheDirectory&nbsp;</span><br>
Location of all cache and temporary files (auto-generated by the script
engine) for the script file being executed. </li>
<li><span style="font-style: italic;">ScriptFile</span><span style="font-style: italic;"> (obsolete)</span><br>
The name of the script file&nbsp;being executed.<span style="font-style: italic;"></span></li>
<li><span style="font-style: italic;">PrimaryScriptFile </span><span style="font-style: italic;">(obsolete)</span><br>
The name of the primary script file. It is the same as the name of the
script being executed, except for the scripts, which are subject of the
pre/post execution actions. In this case it is the name of the parent
script containing pre/post execution directives.</li>
</ul>
<div style="margin-left: 40px;"><span style="font-weight: bold;">Methods</span></div>
<ul>
<li><span style="font-style: italic;">string
GetCacheDirectory(string file)</span><br>
Returns the name of the cache directory for the given script file.<br>
</li>
</ul>
<span style="font-style: italic;">Because </span><span style="font-style: italic; color: rgb(51, 51, 153);">CSSEnvironment</span><span style="font-style: italic;"> interface may vary from version
to version see CSScriptLibrary help in&nbsp;</span><a style="font-style: italic;" href="ProgrammingReference.html">Programming
Reference</a><span style="font-style: italic;"> for
details .</span><span style="font-style: italic;"></span><br>
<br><span style="font-style: italic; font-weight: bold;">NOTE:
ScriptFile and PrimaryScriptFile are made obsolete as they may not work
correctly in the hosted and cached scenarios. Use alternative
techniques demonstrated in theReflectScript.cs sample.&nbsp;Including
environment variable 'EntryScript' and AssemblyDescriptionAttribute (of
the script assembly) containing the full path of the script file.</span><br style="font-style: italic; font-weight: bold;"><br>
The full name of the script being executed can also be discovered in a
very simple way, which does not require loading any external
dependency (<span style="font-style: italic;">CSScriptLibrary.dll</span>).
Every script being excuted has its assembly stamped with the
AssemblyDescriptionAttribute value of which is set to the full script
file name. <br>
<br>

<table style="width: 710px; text-align: left; background-color: rgb(255, 255, 204);" border="1" cellpadding="2" cellspacing="2">

<tbody><tr><td style="white-space: nowrap;"><pre style="font-family: Consolas; font-size: 13px; color: black; background-image: none; background-repeat: repeat; background-attachment: scroll; background-position: 0% 50%;"><span style="color: blue;"></span><span style="color: blue;">var</span>&nbsp;scriptFile&nbsp;=&nbsp;assembly.GetCustomAttributes(<span style="color: blue;">typeof</span>(<span style="color: rgb(43, 145, 175);">AssemblyDescriptionAttribute</span>),&nbsp;<span style="color: blue;">true</span>)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Cast&lt;<span style="color: rgb(43, 145, 175);">AssemblyDescriptionAttribute</span>&gt;()<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.First()<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Description;</pre>
</td>
    </tr>
  </tbody>
</table>


<br>
<span style="font-style: italic; color: black;">More samples on how
obtain the script name from the script can be found in the
ReflectScript sample (&lt;cs-script&gt;\Samples\ReflectScript.cs).</span><span style="font-style: italic; font-weight: bold;"><br>
<br>
<br>
</span><br>
In addition to this the script engine always sets at startup some
environment variables for current process:<br>
<ul>
<li>CSScriptRuntime - version of the script engine.&nbsp;</li>
<li>CSScriptRuntimeLocation - location&nbsp;of the script engine.&nbsp;</li><li>CSScriptDebugging - name and version of the debugger/IDE<br>
</li>
</ul>
This can be useful when you need to know at run time if the execution
is under CS-Script environment&nbsp;and/or IDE. The following code
illustrates this:<br>
<br>
<table style="width: 710px; text-align: left;" border="1" cellpadding="2" cellspacing="2">
<tbody>
<tr>
<td style="white-space: nowrap; background-color: rgb(255, 255, 204);"><font face="Courier New" size="2"><font color="#0000ff" size="2">using</font>&nbsp;System;<br>
<br>
<font color="#0000ff" size="2">class</font>&nbsp;Script<br>
{<br>
&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff" size="2">static</font>&nbsp;<font color="#0000ff" size="2">public</font>&nbsp;<font color="#0000ff" size="2">void</font>&nbsp;Main(<font color="#0000ff" size="2">string</font>[]&nbsp;args)<br>
&nbsp;&nbsp;&nbsp;&nbsp;{<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff" size="2">string</font>&nbsp;cssRuntime&nbsp;=&nbsp;Environment.GetEnvironmentVariable(<font color="#800000" size="2">"CSScriptRuntime"</font>);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff" size="2">if</font>&nbsp;(cssRuntime&nbsp;!=&nbsp;null)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine(<font color="#800000" size="2">"Running&nbsp;under&nbsp;CS-Script&nbsp;v"</font>&nbsp;+&nbsp;cssRuntime.ToString());<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff" size="2">string</font>&nbsp;cssDebugging&nbsp;=&nbsp;Environment.GetEnvironmentVariable(<font color="#800000" size="2">"CSScriptDebugging"</font>);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff" size="2">if</font>&nbsp;(cssDebugging&nbsp;!=&nbsp;null)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine(<font color="#800000" size="2">"Debugging&nbsp;under&nbsp;"</font>&nbsp;+&nbsp;cssDebugging.ToString());<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff" size="2">else</font><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine(<font color="#800000" size="2">"Running&nbsp;as&nbsp;stand-alone&nbsp;application"</font>);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>
&nbsp;&nbsp;&nbsp;&nbsp;}<br>
}</font></td>
</tr>
</tbody>
</table>
<br>
<a id="_css_project_symbol" name="_css_project_symbol">
In addition to the environment variables you can also detect "under
IDE" (e.g. Visual Studio) execution with the compiler symbol&nbsp;CSS_PROJECT:<br>
<br>
</a>
<table style="text-align: left; width: 732px; height: 60px;" border="1" cellpadding="2" cellspacing="2">
<tbody>
<tr>
<td style="white-space: nowrap; background-color: rgb(255, 255, 204);"><font face="Courier New" size="2"><font color="#0000ff" size="2">#if</font>&nbsp;CSS_PROJECT<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine(<font color="#800000" size="2">"Script&nbsp;is&nbsp;opened&nbsp;with&nbsp;Visual&nbsp;Studio."</font>);<br>
<font color="#0000ff" size="2">#endif<br>
</font></font></td>
</tr>
</tbody>
</table>
<br>
<a id="_script_assembly_attribute" name="_script_assembly_attribute">
In script hosting scenarios it can be useful to distinguish
"scripted"code from "static" one.
</a>This can be useful when you need to know at run
time if the methed implemented by the host application is invoked by
the Host or Script routine. &nbsp;CS-Script engine stamps every
compiled script assembly with the&nbsp;<span style="color: rgb(0, 0, 153);">AssemblyDescriptionAttribute</span>
custom attribute containing the script file as attribute <span style="font-style: italic;">Description</span> property. Thus you can always analyse your <span style="color: rgb(0, 0, 153);">StackFrame</span> and
check if the calling assembly is the script being executed.
&nbsp;To simlify this task <span style="color: rgb(0, 0, 153);">CSScriptLibrary</span>
implements Assembly extension method&nbsp;<span style="color: rgb(0, 0, 153);">GetScriptName()</span>.<br>
<br>
<table style="width: 710px; text-align: left;" border="1" cellpadding="2" cellspacing="2">
<tbody>
<tr>
<td style="white-space: nowrap; background-color: rgb(255, 255, 204);"><font face="Courier New" size="2"><font color="#0000ff" size="2">public</font>&nbsp;<font color="#0000ff" size="2">void</font>&nbsp;</font><font face="Courier New" size="2">SomeHostMethod()<br>
{<br>
&nbsp; &nbsp;&nbsp;</font><font face="Courier New" size="2"><font color="#0000ff" size="2">if</font></font><font face="Courier New" size="2">
(Assembly.GetCallingAssembly().GetScriptName() != null)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
Console.WriteLine(</font><font face="Courier New" size="2"><font color="#800000" size="2">"Invoked
from the script..."</font></font><font face="Courier New" size="2">);<br>
&nbsp;&nbsp;&nbsp;&nbsp;</font><font face="Courier New" size="2"><font color="#0000ff" size="2">else</font></font><br>
<font face="Courier New" size="2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font face="Courier New" size="2">Console.WriteLine(</font><font face="Courier New" size="2"><font color="#800000" size="2">"Invoked from the host..."</font></font><font face="Courier New" size="2">);</font><font face="Courier New" size="2"><br>
}</font></td>
</tr>
</tbody>
</table>
<br>
<br>
<h4 class="dtH4"><a id="_css_project_symbol" name="_css_project_symbol">See Also&nbsp;</a></h4>
<p><a href="AppConfig.html">CS-Script application
configuration</a>&nbsp;</p>
</div>
</body></html>